import { sport_map } from '../../utils/dataset/db_data';
import { devDebug } from '../../utils/settings';

Page({
  data: {
    loading: 0,
    refresher: 0,
    stadiumList: [],
  },
  onLoad(options) {
    if (!devDebug) this.loadStadiumList();
  },
  async loadStadiumList() {
    this.setData({ loading: 1 });
    let resList = await new Promise(resolve => {
      wx.cloud.callFunction({
        name: 'db_stadium',
        data: {
          funcName: 'query',
          searchText: '',
          searchData: { stadium_state: 1 }
        },
        success: (r) => resolve(r.result),
      });
    });
    resList = resList.map(o => {
      o.appointment_period_text = `可提前 ${o.appointment_period} 天预定`;
      o.open_time = o.start_time + ' ~ ' + o.end_time;
      o.sport_list = o.sport_ids.map(v => ({ _id: v, sport_name: sport_map.get(v) }));
      return o;
    });
    if(devDebug) console.log(resList);
    this.setData({ loading: 0, refresher: 0, stadiumList: resList });
  },
  onNavigate(ev) {
    const idx = ev.currentTarget.dataset.idx;
    const _data = this.data.stadiumList[idx];
    const seenFields = ['appointment_period', 'fig_urls', 'open_time', 'sport_list', 'stadium_addr', 'stadium_desc', 'stadium_name', 'stadium_phone', '_id', 'stadium_geo'];
    const stadiumInfo = {};
    seenFields.forEach(k => stadiumInfo[k] = _data[k]);
    wx.navigateTo({
      url: `/pages/reservation/stadium?stadiumInfo=${JSON.stringify(stadiumInfo)}`,
    })
  },
  onPullDownRefresh() {
    this.loadStadiumList();
  },
})